From ab0eb210ee4972b8979a3896d1643c8a7fb9b51c Mon Sep 17 00:00:00 2001 From: robertlipe Date: Sat, 9 Mar 2013 21:45:22 +0000 Subject: [PATCH] Pencil-whipped the remaining OLDGPX cases and convinced myself we'd delivered parity. Removed fprintf style output totally in favor in Qt XML serializer. --- gpsbabel/garmin_fs.cc | 86 +----------------------- gpsbabel/garmin_fs.h | 2 +- gpsbabel/gpx.cc | 148 +++++++----------------------------------- 3 files changed, 26 insertions(+), 210 deletions(-) diff --git a/gpsbabel/garmin_fs.cc b/gpsbabel/garmin_fs.cc index 38efa926d..3e6491bea 100644 --- a/gpsbabel/garmin_fs.cc +++ b/gpsbabel/garmin_fs.cc @@ -185,7 +185,7 @@ void garmin_fs_convert(void* fs) /* GPX - out */ void -garmin_fs_xml_fprint(gbfile* ofd, const waypoint* waypt, +garmin_fs_xml_fprint(const waypoint* waypt, QXmlStreamWriter& writer) { const char* phone, *addr; @@ -218,11 +218,6 @@ garmin_fs_xml_fprint(gbfile* ofd, const waypoint* waypt, WAYPT_HAS(waypt, proximity) || WAYPT_HAS(waypt, temperature) || gmsd->flags.display) { - int space = 1; -#if OLDGPX - gbfprintf(ofd, "%*s\n", space++ * 2, ""); - gbfprintf(ofd, "%*s\n", space++ * 2, "", GARMIN_GPX_EXT_REFERENCE); -#else writer.writeStartElement("extensions"); writer.writeStartElement("gpxx:WaypointExtension"); writer.writeNamespace("http://www.garmin.com/xmlschemas/GpxExtensions/v3", @@ -232,27 +227,14 @@ garmin_fs_xml_fprint(gbfile* ofd, const waypoint* waypt, writer.writeAttribute("xsi:schemaLocation", "http://www.garmin.com/xmlschemas/GpxExtensions/v3 " "http://www.garmin.com/xmlschemas/GpxExtensions/v3/GpxExtensionsv3.xsd"); -#endif if WAYPT_HAS(waypt, proximity) { -#if OLDGPX - gbfprintf(ofd, "%*s%.6f\n", space * 2, "", waypt->proximity); -#else writer.writeTextElement("gpxx:Proximity", QString::number(waypt->proximity, 'f', 6)); -#endif } if WAYPT_HAS(waypt, temperature) { -#if OLDGPX - gbfprintf(ofd, "%*s%.6f\n", space * 2, "", waypt->temperature); -#else writer.writeTextElement("gpxx:Temperature", QString::number(waypt->temperature, 'f', 6)); -#endif } if WAYPT_HAS(waypt, depth) { -#if OLDGPX - gbfprintf(ofd, "%*s%.6f\n", space * 2, "", waypt->depth); -#else writer.writeTextElement("gpxx:Depth", QString::number(waypt->depth, 'f', 6)); -#endif } if (gmsd->flags.display) { const char* cx; @@ -267,114 +249,48 @@ garmin_fs_xml_fprint(gbfile* ofd, const waypoint* waypt, cx = "SymbolAndName"; break; } -#if OLDGPX - gbfprintf(ofd, "%*s%s\n", space * 2, "", cx); -#else writer.writeTextElement("gpxx:DisplayMode", cx); -#endif } if (gmsd->flags.category && gmsd->category) { int i; gbuint16 cx = gmsd->category; -#if OLDGPX - gbfprintf(ofd, "%*s\n", space++ * 2, ""); -#else writer.writeStartElement("gpxx:Categories"); -#endif for (i = 0; i < 16; i++) { if (cx & 1) { -#if OLDGPX - gbfprintf(ofd, "%*sCategory %d\n", space*2, "", i+1); -#else writer.writeTextElement("gpxx:Category", QString("Category %1").arg(i+1)); -#endif } cx = cx >> 1; } -#if OLDGPX - gbfprintf(ofd, "%*s\n", --space * 2, ""); -#else writer.writeEndElement(); // gpxx:Categories -#endif } if (*addr) { char* str; -#if OLDGPX - char* tmp; - gbfprintf(ofd, "%*s\n", space++ * 2, ""); -#else writer.writeStartElement("gpxx:Address"); -#endif if ((str = GMSD_GET(addr, NULL))) { -#if OLDGPX - tmp = xml_entitize(str); - gbfprintf(ofd, "%*s%s\n", space * 2, "", tmp); - xfree(tmp); -#else writer.writeTextElement("gpxx:StreetAddress", str); -#endif } if ((str = GMSD_GET(city, NULL))) { -#if OLDGPX - tmp = xml_entitize(str); - gbfprintf(ofd, "%*s%s\n", space * 2, "", tmp); - xfree(tmp); -#else writer.writeTextElement("gpxx:City", str); -#endif } if ((str = GMSD_GET(state, NULL))) { -#if OLDGPX - tmp = xml_entitize(str); - gbfprintf(ofd, "%*s%s\n", space * 2, "", tmp); - xfree(tmp); -#else writer.writeTextElement("gpxx:State", str); -#endif } if ((str = GMSD_GET(country, NULL))) { -#if OLDGPX - tmp = xml_entitize(str); - gbfprintf(ofd, "%*s%s\n", space * 2, "", tmp); - xfree(tmp); -#else writer.writeTextElement("gpxx:Country", str); -#endif } if ((str = GMSD_GET(postal_code, NULL))) { -#if OLDGPX - tmp = xml_entitize(str); - gbfprintf(ofd, "%*s%s\n", space * 2, "", tmp); - xfree(tmp); -#else writer.writeTextElement("gpxx:PostalCode", str); -#endif } -#if OLDGPX - gbfprintf(ofd, "%*s\n", --space * 2, ""); -#else writer.writeEndElement(); // /gpxx::Address -#endif } if (*phone) { -#if OLDGPX - char* tmp = xml_entitize(phone); - gbfprintf(ofd, "%*s%s\n", space * 2, "", tmp); - xfree(tmp); -#else writer.writeTextElement("gpxx:PhoneNumber", phone); -#endif } -#if OLDGPX - gbfprintf(ofd, "%*s\n", --space * 2, ""); - gbfprintf(ofd, "%*s\n", --space * 2, ""); -#else writer.writeEndElement(); // /gpxx::WaypointExtension writer.writeEndElement(); // /extensions. -#endif } } diff --git a/gpsbabel/garmin_fs.h b/gpsbabel/garmin_fs.h index a050c21af..95b4e8e5c 100644 --- a/gpsbabel/garmin_fs.h +++ b/gpsbabel/garmin_fs.h @@ -123,7 +123,7 @@ char* garmin_fs_xstrdup(const char* src, size_t size); /* for GPX */ void garmin_fs_xml_convert(const int base_tag, int tag, const char* cdatastr, waypoint* waypt); class QXmlStreamWriter; -void garmin_fs_xml_fprint(gbfile* ofd, const waypoint* waypt, QXmlStreamWriter&); +void garmin_fs_xml_fprint(const waypoint* waypt, QXmlStreamWriter&); /* common garmin_fs utilities */ diff --git a/gpsbabel/gpx.cc b/gpsbabel/gpx.cc index 708bee58d..093fa68e0 100644 --- a/gpsbabel/gpx.cc +++ b/gpsbabel/gpx.cc @@ -289,7 +289,6 @@ gpx_write_gdata(gpx_global_entry* ge, const char* tag) (0 == strcmp(tag, "email"))) { break; } - gbfprintf(ofd, " "); } writer.writeEndElement(); } @@ -1378,8 +1377,11 @@ gpx_wr_init(const char* fname) ofd = gbfopen(fname, "w", MYNAME); writer.setAutoFormattingIndent(2); -// writer.setCodec("UTF-8"); -// writer.writeStartDocument(); + // Technically, XML (and therefore GPX) defaults ot UTF-8, so we should not + // have to declare this. For compatibility with the existing Qt writer, + // we do... + // writer.setCodec("UTF-8"); + // writer.writeStartDocument(); writer.writeProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\""); } @@ -1535,31 +1537,21 @@ fprint_xml_chain(xml_tag* tag, const waypoint* wpt) } if (wpt && wpt->gc_data->exported && strcmp(tag->tagname, "groundspeak:cache") == 0) { - xml_write_time(ofd, wpt->gc_data->exported, 0, - "groundspeak:exported"); + char time_string[64]; + xml_fill_in_time(time_string, wpt->gc_data->exported, + 0, XML_LONG_TIME); + if (time_string[0]) { + writer.writeTextElement("time", time_string); + } } writer.writeEndElement(); } if (tag->parentcdata) { -#if OLDGPX - // retain whitespacing, but nuke leading NL as the above will add a trailing. - - char* otmp_ent, *tmp_ent = NULL; - otmp_ent = xml_entitize(tag->parentcdata); - if (otmp_ent[0] == '\n') { - tmp_ent = otmp_ent+1; - } else { - tmp_ent = otmp_ent; - } - gbfprintf(ofd, "%s", tmp_ent); - xfree(otmp_ent); -#else // FIXME: The length check is necessary to get line endings correct in our test suite. // Writing the zero length string eats a newline, at least with Qt 4.6.2. if (tag->parentcdatalen > 0) { writer.writeCharacters(tag->parentcdata); } -#endif } tag = tag->sibling; } @@ -1609,28 +1601,6 @@ write_gpx_url(const waypoint* waypointp) return; } -#if OLDGPX - char* tmp_ent; - if (gpx_wversion_num > 10) { - url_link* tail; - for (tail = (url_link*)&waypointp->url_next; tail; tail = tail->url_next) { - tmp_ent = xml_entitize(tail->url); - gbfprintf(ofd, " \n", - urlbase ? urlbase : "", tmp_ent); - write_optional_xml_entity(ofd, " ", "text", - tail->url_link_text); - gbfprintf(ofd, " \n"); - xfree(tmp_ent); - } - } else { - tmp_ent = xml_entitize(waypointp->url); - gbfprintf(ofd, " %s%s\n", - urlbase ? urlbase : "", tmp_ent); - write_optional_xml_entity(ofd, " ", "urlname", - waypointp->url_link_text); - xfree(tmp_ent); - } -#else if (gpx_wversion_num > 10) { url_link* tail; for (tail = (url_link*)&waypointp->url_next; tail; tail = tail->url_next) { @@ -1647,7 +1617,6 @@ write_gpx_url(const waypoint* waypointp) } writer.writeTextElement("url", QString(urlbase) + QString(waypointp->url)); writer.writeOptionalTextElement("urlname", QString(waypointp->url_link_text)); -#endif } /* @@ -1728,36 +1697,6 @@ gpx_write_common_extensions(const waypoint* waypointp, const char* indent) // FIXME: Although not required by the schema it seems gpxtpx:TrackPointExtension should only be used as a child of gpx:trkpt if (((opt_humminbirdext || opt_garminext) && (waypointp->depth != 0 || waypointp->temperature != 0)) || (opt_garminext && (waypointp->heartrate != 0 || waypointp->cadence != 0))) { -#if OLDGPX - gbfprintf(ofd, "%s\n", indent); - if (waypointp->depth != 0) { - if (opt_humminbirdext) - gbfprintf(ofd, "%s %f\n", - indent, waypointp->depth*100.0); - if (opt_garminext) - gbfprintf(ofd, "%s %f\n", - indent, waypointp->depth); - } - if (waypointp->temperature != 0) { - if (opt_humminbirdext) - gbfprintf(ofd, "%s %f\n", - indent, waypointp->temperature); - if (opt_garminext) - gbfprintf(ofd, "%s %f\n", - indent, waypointp->temperature); - } - if (opt_garminext && (waypointp->heartrate != 0 || waypointp->cadence != 0)) { - gbfprintf(ofd, "%s \n", indent); - if (waypointp->heartrate != 0) - gbfprintf(ofd, "%s %u\n", - indent, waypointp->heartrate); - if (waypointp->cadence != 0) - gbfprintf(ofd, "%s %u\n", - indent, waypointp->cadence); - gbfprintf(ofd, "%s \n", indent); - } - gbfprintf(ofd, "%s\n", indent); -#else writer.writeStartElement("extensions"); if (waypointp->depth != 0) { if (opt_humminbirdext) { @@ -1786,7 +1725,6 @@ gpx_write_common_extensions(const waypoint* waypointp, const char* indent) writer.writeEndElement(); // "gpxtpx:TrackPointExtension" } writer.writeEndElement(); // "extensions" -#endif } } @@ -1833,7 +1771,7 @@ gpx_waypt_pr(const waypoint* waypointp) } if (gmsd && (gpx_wversion_num > 10)) { /* MapSource doesn't accepts extensions from 1.0 */ - garmin_fs_xml_fprint(ofd, waypointp, writer); + garmin_fs_xml_fprint(waypointp, writer); } } else { gpx_write_common_extensions(waypointp, " "); @@ -1846,29 +1784,22 @@ gpx_track_hdr(const route_head* rte) { fs_xml* fs_gpx; current_trk_head = rte; -#if OLDGPX - gbfprintf(ofd, "\n"); - write_optional_xml_entity(ofd, " ", "name", rte->rte_name); - write_optional_xml_entity(ofd, " ", "desc", rte->rte_desc); + + writer.writeStartElement("trk"); + writer.writeOptionalTextElement("name", rte->rte_name); + writer.writeOptionalTextElement("desc", rte->rte_desc); if (rte->rte_num) { - gbfprintf(ofd, "%d\n", rte->rte_num); + writer.writeTextElement("number", QString::number(rte->rte_num)); } + // FIXME: Note that this chain nonsense is not executed in our + // test suite as of 2013-03-09. if (gpx_wversion_num > 10) { fs_gpx = (fs_xml*)fs_chain_find(rte->fs, FS_GPX); if (fs_gpx) { fprint_xml_chain(fs_gpx->tag, NULL); } } -#else - writer.writeStartElement("trk"); - writer.writeOptionalTextElement("name", rte->rte_name); - writer.writeOptionalTextElement("desc", rte->rte_desc); - if (rte->rte_num) { - writer.writeTextElement("number", QString::number(rte->rte_num)); - } - // TODO the chain nonsense. -#endif } static void @@ -2071,12 +2002,6 @@ gpx_write(void) fatal(MYNAME ": gpx version number of '%s' not valid.\n", gpx_wversion); } -#if OLDGPX - gbfprintf(ofd, "\n", global_opts.charset_name); - gbfprintf(ofd, " and to pass // diff -w. It's here for now to shim compatibility with our zillion @@ -2088,47 +2013,26 @@ gpx_write(void) writer.writeAttribute("\n version", gpx_wversion); writer.writeAttribute("\n creator", CREATOR_NAME_URL); writer.writeAttribute("\n xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); -#endif - // FIXME: If we pass elements from an optional declared namespace, e.g. gpxx, gpxtpx, or h through with fprint_xml_chain, we won't have declared the namespace. + // FIXME: If we pass elements from an optional declared namespace, + // e.g. gpxx, gpxtpx, or h through with fprint_xml_chain, we won't + // have declared the namespace. // This is a schema violation. if (opt_humminbirdext) { -#if OLDGPX - gbfprintf(ofd, " xmlns:h=\"http://humminbird.com\"\n"); -#else writer.writeAttribute("\n xmlns:h","http://humminbird.com"); -#endif } if (opt_garminext) { -#if OLDGPX - gbfprintf(ofd, " xmlns:gpxx=\"http://www.garmin.com/xmlschemas/GpxExtensions/v3\"\n" - " xmlns:gpxtpx=\"http://www.garmin.com/xmlschemas/TrackPointExtension/v1\"\n"); -#else + // TODO: it's kind of wrong to force formatting in here... writer.writeAttribute("\n xmlns:gpxx", "http://www.garmin.com/xmlschemas/GpxExtensions/v3"); writer.writeAttribute("\n xmlns:gpxtpx", "http://www.garmin.com/xmlschemas/TrackPointExtension/v1"); -#endif } -#if OLDGPX - gbfprintf(ofd, " xmlns=\"http://www.topografix.com/GPX/%c/%c\"\n", gpx_wversion[0], gpx_wversion[2]); -#else writer.writeAttribute("\n xmlns", QString("http://www.topografix.com/GPX/%1/%2").arg(gpx_wversion[0]).arg(gpx_wversion[2])); -#endif if (xsi_schema_loc) { -#if OLDGPX - gbfprintf(ofd, " xsi:schemaLocation=\"%s\">\n", xsi_schema_loc); -#else + // FIXME: it's tacky to have formatting in here... writer.writeAttribute("\n xsi:schemaLocation", xsi_schema_loc); -#endif } else { -#if OLDGPX - gbfprintf(ofd, - " xsi:schemaLocation=" DEFAULT_XSI_SCHEMA_LOC_FMT">\n", - gpx_wversion[0], gpx_wversion[2], - gpx_wversion[0], gpx_wversion[2]); -#else writer.writeAttribute("\n xsi:schemaLocation", QString("http://www.topografix.com/GPX/%1/%2 http://www.topografix.com/GPX/%1/%2/gpx.xsd").arg(gpx_wversion[0]).arg(gpx_wversion[2])); -#endif } if (gpx_wversion_num > 10) { @@ -2150,15 +2054,11 @@ gpx_write(void) } now = current_time(); -#if OLDGPX - xml_write_time(ofd, now, 0, "time"); -#else char time_string[64]; xml_fill_in_time(time_string, now, 0, XML_LONG_TIME); if (time_string[0]) { writer.writeTextElement("time", time_string); } -#endif gpx_write_gdata(&gpx_global->keywords, "keywords"); gpx_write_bounds(); -- 2.30.2